﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using EventMgrLib;
using SmartMotion.Core;
using WeifenLuo.WinFormsUI.Docking;

namespace SmartMotion.Pages.DockPages
{
    public partial class FrmDebug : DockContent
    {
        private EventMgr _em;
        public FrmDebug()
        {
            InitializeComponent();
        }

        private void FrmDebug_Load(object sender, EventArgs e)
        {
            HideOnClose = true;
            _em = EventMgr.Ins;
            _em.GetEvent<DebugMessage>().Subscribe(ShowMessage);
        }

        private void ShowMessage(string message)    
        {
            if (InvokeRequired)
            {
                BeginInvoke(new Action(() => ShowMessage(message)));
            }
            else
            {
                rtbDebug.AppendText($"{DateTime.Now} {message}\r");
                //_em.GetEvent<SelectedObjectChanged>().Publish(this);
            }
        }   

        private void clearToolStripMenuItem_Click(object sender, EventArgs e)
        {
            rtbDebug.Clear();
        }

        private void rtbDebug_TextChanged(object sender, EventArgs e)
        {
            if (rtbDebug.TextLength>10000)
            {
                rtbDebug.Clear();
            }

            rtbDebug.SelectionStart = rtbDebug.Text.Length;
            rtbDebug.ScrollToCaret();
        }

        private void FrmDebug_FormClosing(object sender, FormClosingEventArgs e)
        {
            _em.GetEvent<DebugMessage>().Unsubscribe(ShowMessage);
        }
    }
}
